package com.sip.stream.activity.preview;

import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.SeekBar;
import androidx.appcompat.app.AppCompatActivity;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.sip.stream.R;
import com.sip.stream.activity.preview.model.PreviewStaticDataViewModel;
import com.sip.stream.activity.preview.model.PreviewViewModel;
import com.sip.stream.base.BaseFragment;
import com.sip.stream.databinding.FragmentPreviewBinding;
import com.sip.stream.rxbus.RxBus;
import com.sip.stream.utils.RxViewBinder;
import com.sip.stream.utils.zfy.PreviewDetectedBean;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class PreviewFragment extends BaseFragment<FragmentPreviewBinding> {

    private static final String TAG = "PreviewFragment";

    private CompositeDisposable compositeDisposable;
    private Disposable controllerLayoutDisplay;
    private Disposable needWakeScreenDisposable;
    private PreviewBusinessController previewBusinessController;
    private int MAXZOOM = 15;
    private final Format previewDateFormate = new SimpleDateFormat("HH:mm:ss");
    private Disposable refreshTimeDisposable = null;


    @Override
    public void initPage() {
        setUp();
    }

    public static PreviewFragment newInstance() {
        return new PreviewFragment();
    }

    private void setUp() {
        PreviewBusinessController previewBusinessController = new PreviewBusinessController((AppCompatActivity) getActivity());
        this.previewBusinessController = previewBusinessController;
        previewBusinessController.setUpTextureView(this.binding.layoutPreview, this.binding.preview, this.binding.previewDrawView);
        getLifecycle().addObserver(this.previewBusinessController);
        this.previewBusinessController.setUpStatus(this.binding.includeStatusLayout);
        //this.previewBusinessController.setUpFrontTextureView(this.binding.layoutPipPreview);
        bindViews();
        Log.i(TAG, "getPath " + Environment.getExternalStorageDirectory().getPath());
        Log.i(TAG, "getAbsolutePath " + Environment.getExternalStorageDirectory().getAbsolutePath());
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
        refreshTime(true);
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
        refreshTime(false);
    }



    public  void m689x34d9a0cd(View view) {
        showControllerLayout();
    }

    private void needWakeScreen() {
        Disposable disposable = this.needWakeScreenDisposable;
        if (disposable != null) {
            disposable.dispose();
            this.needWakeScreenDisposable = null;
            return;
        }
        Observable.interval(3L, TimeUnit.SECONDS).doOnNext(new Consumer() {
            @Override
            public  void accept(Object obj) {
                try {
                    Runtime.getRuntime().exec("input keyevent 26");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).subscribe(new Observer<Long>() {
            @Override
            public void onComplete() {
            }

            @Override
            public void onError(Throwable th) {
            }

            @Override
            public void onNext(Long l) {
            }

            @Override
            public void onSubscribe(Disposable disposable2) {
                PreviewFragment.this.needWakeScreenDisposable = disposable2;
            }
        });
    }


    private void bindViews() {
        if (this.compositeDisposable == null) {
            this.compositeDisposable = new CompositeDisposable();
        }
        PreviewViewModel viewObserver = this.previewBusinessController.getViewObserver();
        viewObserver.previewStaticDataSubject.observeOn(AndroidSchedulers.mainThread()).doOnNext(new Consumer() {
            @Override
            public  void accept(Object obj) {
                PreviewStaticDataViewModel previewStaticDataViewModel = (PreviewStaticDataViewModel) obj;
                //binding.layoutRecording.setVisibility(previewStaticDataViewModel.showDate ? View.GONE : View.VISIBLE);
            }
        }).subscribe(new Observer<PreviewStaticDataViewModel>() {
            @Override
            public void onComplete() {
            }

            @Override
            public void onNext(PreviewStaticDataViewModel previewStaticDataViewModel) {
            }

            @Override
            public void onSubscribe(Disposable disposable) {
                PreviewFragment.this.compositeDisposable.add(disposable);
            }

            @Override
            public void onError(Throwable th) {
                th.printStackTrace();
            }
        });
        RxViewBinder.bind(viewObserver.getNowTime(),this.binding.tvTime, this.compositeDisposable);
        RxViewBinder.bindTextViewColor(viewObserver.getTvStorageColor(), this.binding.tvStorage, this.compositeDisposable);
        RxViewBinder.bind(viewObserver.getTvStorage(), this.binding.tvStorage, this.compositeDisposable);
        RxViewBinder.bind(viewObserver.getPpiStr(), this.binding.tvPpi, this.compositeDisposable);
        RxViewBinder.bind(viewObserver.getGpsStr(), this.binding.tvGps, this.compositeDisposable);
        viewObserver.toastObservable().observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<String>() {
            @Override
            public void onComplete() {
            }

            @Override
            public void onSubscribe(Disposable disposable) {
                PreviewFragment.this.addDisposable(disposable);
            }

            @Override
            public void onNext(String message) {
                ToastUtils.showShort(message);
            }

            @Override
            public void onError(Throwable th) {
                th.printStackTrace();
            }
        });
        viewObserver.getDetectedBeanBehaviorSubject().observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<PreviewDetectedBean>() {
            @Override
            public void onComplete() {
            }

            @Override
            public void onError(Throwable th) {
            }

            @Override
            public void onSubscribe(Disposable disposable) {
                PreviewFragment.this.compositeDisposable.add(disposable);
            }

            @Override
            public void onNext(PreviewDetectedBean previewDetectedBean) {
                PreviewFragment.this.drawRect(previewDetectedBean);
            }
        });
        /*(this.binding).seekbarZoom.incrementProgressBy(1);
        (this.binding).seekbarZoom.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean z) {
                float progress = seekBar.getProgress() / 100.0f;
                (PreviewFragment.this.binding).tvShowZoom.setText(String.format("%.1fX", Double.valueOf((PreviewFragment.this.MAXZOOM * progress) + 1.0d)));
                if (z) {
                    PreviewFragment.this.previewBusinessController.cameraZoomPercent(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                if (PreviewFragment.this.controllerLayoutDisplay != null) {
                    PreviewFragment.this.controllerLayoutDisplay.dispose();
                    PreviewFragment.this.controllerLayoutDisplay = null;
                }
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                (PreviewFragment.this.binding).tvZoom.setText(String.format("%.1fX", Double.valueOf(((seekBar.getProgress() / 100.0f) * PreviewFragment.this.MAXZOOM) + 1.0d)));
                PreviewFragment.this.hideControllerLayout();
            }
        });*/
    }

    public void drawRect(PreviewDetectedBean previewDetectedBean) {
        if (previewDetectedBean == null) {
            return;
        }
        this.binding.previewDrawView.addDetectedBean(previewDetectedBean);
    }

    public void handleMediaAction(String action) {
        Log.i(TAG, "handleMediaAction " + action);
        this.previewBusinessController.handleMediaAction(action);
    }

    public boolean isOpenedCamera() {
        return this.previewBusinessController.isOpenedCamera();
    }

    private void showControllerLayout() {
        RxBus.getDefault().post(Boolean.FALSE, "MainLauncherActivity");
        TranslateAnimation translateAnimation = new TranslateAnimation(1, 0.0f, 1, 0.0f, 1, 2.0f, 1, 0.0f);
        translateAnimation.setDuration(300L);
        //this.binding.layoutController.startAnimation(translateAnimation);
        //this.binding.layoutController.setVisibility(View.VISIBLE);
        hideControllerLayout();
    }

    public void hideControllerLayout() {
        if (this.controllerLayoutDisplay != null) {
            this.controllerLayoutDisplay.dispose();
            this.controllerLayoutDisplay = null;
        }
        Single.just(Boolean.TRUE).delay(3L, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleObserver<Boolean>() {
            @Override
            public void onError(Throwable th) {
            }

            @Override
            public void onSubscribe(Disposable disposable) {
                PreviewFragment.this.controllerLayoutDisplay = disposable;
            }

            @Override
            public void onSuccess(Boolean bool) {
                TranslateAnimation translateAnimation = new TranslateAnimation(1, 0.0f, 1, 0.0f, 1, 0.0f, 1, 2.0f);
                translateAnimation.setDuration(300L);
                //binding.layoutController.startAnimation(translateAnimation);
                //binding.layoutController.setVisibility(View.GONE);
                RxBus.getDefault().post(Boolean.TRUE,"MainLauncherActivity");
            }
        });
    }

    private synchronized void refreshTime(boolean z) {
        if (this.refreshTimeDisposable != null) {
            this.refreshTimeDisposable.dispose();
            this.refreshTimeDisposable = null;
        }
        if (z) {
            Observable.interval(1L, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).doOnNext(new Consumer() {
                @Override
                public  void accept(Object obj) {
                    binding.tvTime.setText(previewDateFormate.format(new Date()));
                    binding.tvRecording.setText(previewBusinessController.getLastString());
                }
            }).doOnDispose(new Action() {
                @Override
                public  void run() {
                    binding.tvTime.setText("");
                    binding.tvRecording.setText("");
                }
            }).subscribe(new Observer<Long>() {
                @Override
                public void onComplete() {
                }

                @Override
                public void onError(Throwable th) {
                }

                @Override
                public void onNext(Long l) {
                }

                @Override
                public void onSubscribe(Disposable disposable) {
                    PreviewFragment.this.refreshTimeDisposable = disposable;
                }
            });
        }
    }

}
